namespace SharpTalk.Core.Services.ServiceAgents
{
    using System;
    using System.ServiceModel;
    using Contracts.Client;
    using Requests;
    using Responses;

    public class MessageDeliveryServiceAgent
        : ClientBase<IMessageServices>, IMessageServices
    {
        
        public MessageDeliveryServiceAgent(string endpointConfigurationName, EndpointAddress remoteAddress) 
            : base(endpointConfigurationName, remoteAddress)
        {
        }

        public IAsyncResult BeginDeliverMessage(MessageSendRequest request, AsyncCallback callback, object state)
        {
            return base.Channel.BeginDeliverMessage(request, callback, state);
        }

        public MessageSendResponse EndDeliverMessage(IAsyncResult result)
        {
            try
            {
                return base.Channel.EndDeliverMessage(result);
            }
            catch (Exception exp)
            {
                return new MessageSendResponse(){ Error = new ErrorDetails(exp)};
            }
        }
    }
}